package com.wenjiang.Interceptor;

import com.wenjiang.config.LanTuPayProperties;
import com.wenjiang.model.request.AbstractWxRequest;
import com.wenjiang.utils.LanTuPaySignUtils;
import lombok.AllArgsConstructor;

import java.util.Map;

/**
 * <p>
 * 设置签名的拦截器
 * </p>
 *
 * @author lwj
 * @since 2024/11/29.
 */
public class HttpRequestSignInterceptor extends AbstractInterceptor{

    public HttpRequestSignInterceptor(LanTuPayProperties lanTuPayProperties) {
        super(lanTuPayProperties);
    }

    @Override
    public Object requestInterceptor(Object request) {
        //获取需要签名的值
        AbstractWxRequest wxRequest = (AbstractWxRequest) request;
        Map<String, String> signParams = wxRequest.signParams();
        String sign = LanTuPaySignUtils.createSign(signParams, lanTuPayProperties.getSecretKey(), new String[0]);
        wxRequest.setSign(sign);

        // 交给下个节点
        HttpInterceptor nextHttpRequestInterceptor = next();
        if (nextHttpRequestInterceptor == null){
            return wxRequest;
        }
        return  nextHttpRequestInterceptor.requestInterceptor(wxRequest);

    }
    @Override
    public Boolean isSupport(Object request) {
        if (request.getClass().isAssignableFrom(AbstractWxRequest.class) ){
            return  true;
        }
        return false;
    }
}
